#include "VideoDecoder.h"

APP_ERROR CallBackVdec(Image& decodedImage, uint32_t channelId, uint32_t frameId, void* userData)
{
    auto data = std::make_shared<Image>(decodedImage);
    auto instance = static_cast<Module*>(userData);
    LogInfo << "Decoder frame:" << frameId;
    instance->SendData(data);
    return APP_ERR_OK;
}

APP_ERROR Decoder::Init() {
    VideoDecodeConfig config;
    config.width = 1280;
    config.height = 720;
    config.callbackFunc = CallBackVdec;
    config.skipInterval = 0; // 跳帧控制
    uint32_t deviceId = 0;
    uint32_t channelId = 0;
    videoDecoder_ = std::make_shared<VideoDecoder>(config, deviceId, channelId);
    return APP_ERR_OK;
    
}

APP_ERROR Decoder::Process(const std::shared_ptr<void> &input) {
    auto image = std::static_pointer_cast<Image>(input);
    auto data = image->GetData();
    APP_ERROR ret = videoDecoder_->Decode(image->GetData(), 
        image->GetDataSize(), frameId_, this);
    frameId_++;
    return ret;
}

APP_ERROR Decoder::DeInit() {
    videoDecoder_.reset();
    return APP_ERR_OK;
}


std::string Decoder::GetModuleName() {
    return "Decoder";
}